<?php
class Common {
	public static function uploadImage($file){
		if(!is_object($file)){
			$name = $file['name'];
			$source = $file['tmp_name'];
		}else{
			$name = $file->name;
			$source = $file->getTempName();
		}
		$result = array('name' => '','link' => '','path' => '');
		$ext = explode('.', $name);
		$path = Yii::app()->basePath . "/../uploaded/photo/";
		$name = md5(md5(time().$name)).'.'.$ext[count($ext)-1];
		if(!file_exists($path.$name)){
			$is_move = move_uploaded_file($source, $path.$name);
			if($is_move){
				$result['name'] = $name;
				$result['link'] = Yii::app()->baseUrl.'/uploaded/photo/'.$name;
				$result['path'] = Yii::app()->basePath . "/../uploaded/photo/".$name;
			}
		}
		return $result;	
	}

	public static function getImagesJson(){
		return array(
			array('thumb' => 'uploaded/photo','image' => 'uploaded/photo','title' => 'sdads','folder' => 'Photo')
		);
	}

	public static function deleteImage($file_name){
		if(empty($file_name)) return false;
		$path = Yii::app()->basePath . "/../uploaded/photo/" . $file_name;
		if(file_exists($path)){
			return unlink($path);
		}
		return false;
	}

	public static function makeSlug($str){
		$str = self::locTiengViet($str);
		$str = preg_replace('/[^a-zA-Z0-9\s]/i', '' , $str);
		$str = preg_replace('/\s+/', '-', $str);
		return $str;
	}

	public static function locTiengViet($str){
		$marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
		"ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
		,"ế","ệ","ể","ễ",
		"ì","í","ị","ỉ","ĩ",
		"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
		,"ờ","ớ","ợ","ở","ỡ",
		"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
		"ỳ","ý","ỵ","ỷ","ỹ",
		"đ",
		"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
		,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
		"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
		"Ì","Í","Ị","Ỉ","Ĩ",
		"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
		,"Ờ","Ớ","Ợ","Ở","Ỡ",
		"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
		"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
		"Đ");


		$marKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
		,"a","a","a","a","a","a",
		"e","e","e","e","e","e","e","e","e","e","e",
		"i","i","i","i","i",
		"o","o","o","o","o","o","o","o","o","o","o","o"
		,"o","o","o","o","o",
		"u","u","u","u","u","u","u","u","u","u","u",
		"y","y","y","y","y",
		"d",
		"A","A","A","A","A","A","A","A","A","A","A","A"
		,"A","A","A","A","A",
		"E","E","E","E","E","E","E","E","E","E","E",
		"I","I","I","I","I",
		"O","O","O","O","O","O","O","O","O","O","O","O"
		,"O","O","O","O","O",
		"U","U","U","U","U","U","U","U","U","U","U",
		"Y","Y","Y","Y","Y",
		"D");
		return str_replace($marTViet,$marKoDau,$str);
	}


	public static function limitWorld($str, $limit){
		$data = preg_split('/(\s+)/i', $str);
		$new_str = array();
		for($i=0;$i<$limit;$i++){
			if(!isset($data[$i])) break;
			$new_str[] = $data[$i];
		}
		return implode($new_str, " ");
	}

	public static function mailValid($email){
		$validator = new CEmailValidator;
		return $validator->validateValue($email);
	}

	public static function showStatusHtml($status = false){
		$class = "label label-important";
		$icon = "<i class='icon-remove'></i>";
		if($status){
			$class = "label label-success";
			$icon = "<i class='icon-ok'></i>";
		}
		return '<span class="'.$class.'">'.$icon.'</span>';
	}
}
?>